package com.member.web.controller.shop;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.member.common.annotation.Log;
import com.member.common.core.controller.BaseController;
import com.member.common.core.domain.AjaxResult;
import com.member.common.core.page.TableDataInfo;
import com.member.common.enums.BusinessType;
import com.member.common.enums.OrderDeleteEnum;
import com.member.common.utils.StringUtils;
import com.member.common.utils.poi.ExcelUtil;
import com.member.shop.domain.*;
import com.member.shop.service.ISysBizActivityService;
import com.member.shop.service.ISysBizCarouselService;
import com.member.shop.service.ISysBizJingangService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 【活动】控制器
 *
 * @author lixinchen
 * @version 1.0
 * @date 2024-01-02
 **/
@RestController
@RequestMapping("/shop/activity")
public class SysBizActivityController extends BaseController {
    @Resource
    private ISysBizActivityService sysBizActivityService;
    private void setTenantId(SysBizActivity sysBizActivity) {
        Long tenantId = getTenantId();
        if (tenantId != null && tenantId > 0) {
            sysBizActivity.setTenantId(tenantId);
        }
    }
    /**
     * 查询活动列表
     */
    @PreAuthorize("@ss.hasPermi('shop:activity:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBizActivity sysBizActivity) {

//        LambdaQueryWrapper<SysBizActivity> queryWrapper = new LambdaQueryWrapper<SysBizActivity>();
//        queryWrapper.like(StringUtils.isNotEmpty(sysBizActivity.getTitle()), SysBizActivity::getTitle, sysBizActivity.getTitle());
//        queryWrapper.eq(sysBizActivity.getStatus()!=null, SysBizActivity::getStatus, sysBizActivity.getStatus());
//        queryWrapper.eq(sysBizActivity.getDisplayed()!=null, SysBizActivity::getDisplayed, sysBizActivity.getDisplayed());
//        queryWrapper.eq(SysBizActivity::getDeleted, OrderDeleteEnum.NOT_DELETE.getKey());
//        queryWrapper.orderByDesc(SysBizActivity::getCreateTime);
        startPage();
        setTenantId(sysBizActivity);
        List<SysBizActivity> list = sysBizActivityService.selectSysBizActivityList(sysBizActivity);
        return getDataTable(list);
    }

    /**
     * 导出活动列表
     */
    @PreAuthorize("@ss.hasPermi('shop:activity:export')")
    @Log(title = "活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBizActivity sysBizActivity) {
        setTenantId(sysBizActivity);
        List<SysBizActivity> list = sysBizActivityService.selectSysBizActivityList(sysBizActivity);
        ExcelUtil<SysBizActivity> util = new ExcelUtil<SysBizActivity>(SysBizActivity.class);
        util.exportExcel(response, list, "活动数据");
    }

    /**
     * 获取活动详细信息
     */
    @PreAuthorize("@ss.hasPermi('shop:activity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysBizActivityService.selectSysBizActivityById(id));
    }

    /**
     * 新增活动
     */
    @PreAuthorize("@ss.hasPermi('shop:activity:add')")
    @Log(title = "活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBizActivity sysBizActivity) {
        return toAjax(sysBizActivityService.insertSysBizActivity(sysBizActivity));
    }

    /**
     * 修改活动
     */
    @PreAuthorize("@ss.hasPermi('shop:activity:edit')")
    @Log(title = "活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBizActivity sysBizActivity) {
        return toAjax(sysBizActivityService.updateSysBizActivity(sysBizActivity));
    }

    @Resource
    private ISysBizCarouselService sysBizCarouselService;

    @Resource
    private ISysBizJingangService sysBizJingangService;
    /**
     * 删除活动
     */
    @PreAuthorize("@ss.hasPermi('shop:activity:remove')")
    @Log(title = "活动", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {

        //查看金刚区是否引用
        LambdaQueryWrapper<SysBizJingang> sysBizJingangLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysBizJingangLambdaQueryWrapper.eq(SysBizJingang::getJumpType,"ACTIVITY_LIST");
        sysBizJingangLambdaQueryWrapper.eq(SysBizJingang::getDeleted,OrderDeleteEnum.NOT_DELETE.getKey());
        List<SysBizJingang> sysBizJingangs = sysBizJingangService.selectSysBizJingangList(sysBizJingangLambdaQueryWrapper);
        if (!CollectionUtils.isEmpty(sysBizJingangs)) {
            for (SysBizJingang sysBizJingang : sysBizJingangs) {
                String jumpContent = sysBizJingang.getJumpContent();
                Pattern pattern = Pattern.compile("id=(\\d+)");
                Matcher matcher = pattern.matcher(jumpContent);
                // 输出匹配到的id值
                if (matcher.find()) {
                    if (Integer.parseInt(matcher.group(1)) == id) {
                        return AjaxResult.warn("该金刚区已分配,不允许删除");
                    }
                }

            }
        }


        //查看轮播图是否引用
        LambdaQueryWrapper<SysBizCarousel> sysBizCarouselLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysBizCarouselLambdaQueryWrapper.eq(SysBizCarousel::getJumpType,"ACTIVITY_LIST");
        sysBizCarouselLambdaQueryWrapper.eq(SysBizCarousel::getDeleted,OrderDeleteEnum.NOT_DELETE.getKey());
        List<SysBizCarousel> sysBizCarousels = sysBizCarouselService.selectSysBizCarouselList(sysBizCarouselLambdaQueryWrapper);
        if (!CollectionUtils.isEmpty(sysBizCarousels)) {
            for (SysBizCarousel sysBizCarousel : sysBizCarousels) {
                String jumpContent = sysBizCarousel.getJumpContent();
                Pattern pattern = Pattern.compile("id=(\\d+)");
                Matcher matcher = pattern.matcher(jumpContent);
                // 输出匹配到的id值
                if (matcher.find()) {
                    if (Integer.parseInt(matcher.group(1)) == id) {
                        return AjaxResult.warn("该轮播图已分配,不允许删除");
                    }
                }

            }
        }

        return toAjax(sysBizActivityService.deleteSysBizActivityById(id));
    }
}
